<?php
namespace Admin\Service;

/**
 * 企业号订阅用户相关功能
 */

use Admin\Model;

class SubscriberService {
     //企业号订阅用户同步状态
    const SYNC_NEED_CREATE = 0; //需要创建用户
    const SYNC_NEED_UPDATE = 1; //需要更新用户
    const SYNC_DOWN = 10; //同步成功
    const SYNC_ERROR_CREATE = 20; //创建用户失败 
    const SYNC_ERROR_UPDATE = 21; //更新用户失败

    const TYPE_CUSTOMER = 1; //客户
    const TYPE_USER = 10; //员工
    const TYPE_VISITOR = 20;//外部访客

    //企业号用户启用与禁用
    const STATUS_ENABLE_ON = 1; //启用
    const STATUS_ENABLE_OFF = 0; //禁用

    //订阅用户状态
    const STATUS_INIT_SERVICE = 0; //0:未服务
    const STATUS_IN_SERVICE = 10; //服务中
    const STATUS_OUT_SERVICE = 20; //已过期
    const STATUS_REFUND_SERVICE = 30; //已退费
    const STATUS_BAN_SERVICE = 40; //已禁用


    //订阅用户性别
    const GENDER_UNKOWN = 0;//未知
    const GENDER_MALE = 1;//男性
    const GENDER_FEMALE = 2;// 女性

    /**
     * 创建新的订阅用户
     * 
    */
    public function add($userId, $name, $phone, $weixin_id, $email, $depart, $qy_code, $contract_id,$gender = self::GENDER_UNKOWN, $type = self::TYPE_CUSTOMER)
    {
        $MSubscriber = D('Admin/QySubscriber');
        $res = array('rcode'=>0, 'msg'=>'500 Internal Error');
        $wheres = $data = array();
        if (!$userId || !$phone || !$weixin_id || !$qy_code || !$contract_id){
            $res['msg'] = '参数错误';
            return $res;
        }

        $MContract = D('Admin/Contract');
        $wherec = array();
        $wherec['contract_id'] = array('eq', $contract_id);
        $dataContract = $MContract->where( $wherec )->find();

        if (!$dataContract){
            $res['msg'] = '合同不存在';
            return $res;
        }
        #TODO  增加合同状态判断，是否合并，是否过过期等
        $data['create_time'] = date('Y-m-d H:i:s');
        $data['sync_message'] = 0;
        $data['qy_subscriber_type'] = $type;
        $data['qy_subscriber_guanzhu'] = 0;
        $data['qy_subscriber_ban'] = 0;
        $data['start_time'] = $dataContract['start_time'];
        $data['end_time'] = $dataContract['end_time'];
        $data['qy_subscriber_status'] = self::STATUS_INIT_SERVICE;
        $data['qy_subscriber_sync'] = self::SYNC_NEED_CREATE;
        $data['qy_subscriber_gender'] = $gender;
        $data['qy_subscriber_userid'] = $userId;
        $data['qy_subscriber_name'] = $name;
        $data['qy_subscriber_mobile'] = $phone;
        $data['qy_subscriber_email'] = $email;
        $data['qy_code'] = $qy_code;
        $data['qy_subscriber_department'] =  implode(',', $depart );
        $data['qy_subscriber_weixinid'] = $weixin_id;
        $data['qy_subscriber_position']= $type==self::TYPE_CUSTOMER ? '学员':($type==self::TYPE_VISITOR ? '合作伙伴':'');

        //订阅用户邮箱、手机、微信号 使用判断
        $wheres['qy_code'] = array('eq', $qy_code);
        $map = array();
        $map['_logic'] = 'OR';
        if ($phone)
            $map['qy_subscriber_mobile'] = array('eq', $data['qy_subscriber_mobile']);
        if ($weixin_id)
            $map['qy_subscriber_weixinid'] = array('eq', $data['qy_subscriber_weixinid']);
        if ($email)
            $map['qy_subscriber_email'] = array('eq', $data['qy_subscriber_email']);
        $wheres['_complex'] = $map;
        $count = $MSubscriber->where( $wheres )->count();
        if ($count){
            $res['msg'] = '订户注册资料已经被使用';
            return $res;
        }
        $addRet = $MSubscriber->data( $data )->add();
        if ($addRet){
            $res['rcode'] = 1;
            $res['msg'] = 'OK';
            $res['data'] = $addRet;
        }
        else{
            $res['msg'] = '订阅用户添加失败';
        }
        return $res;
    }

    /**
     * 现有现有订阅用户增加套餐
     * 
     * 
     * 
    */
    public function addPackage($subscriber_id, $newDepart_id, $contract_id)
    {
        $MSubscriber = D('Admin/QySubscriber');
        $data = array();
        $res = array('rcode'=>0, 'msg'=>'500 Internal Error');
        
        $MContract = D('Admin/Contract');
        $wherec = array();
        $wherec['contract_id'] = array('eq', $contract_id);
        $dataContract = $MContract->where( $wherec )->find();
        if (!$dataContract){
            $res['msg'] = '合同不存在';
            return $res;
        }
        $dataSubscriber = array();
        $wheres = array();
        $wheres['qy_subscriber_id'] = array('eq', $subscriber_id);
        $dataSubscriber = $MSubscriber->where( $wheres )->find();

        $data['start_time'] = $dataContract['start_time'];
        $data['end_time'] = $dataContract['end_time'];
        if ($dataSubscriber['qy_subscriber_sync'] != self::SYNC_NEED_CREATE && $dataSubscriber['qy_subscriber_sync'] != self::SYNC_ERROR_CREATE){
            $data['qy_subscriber_sync'] = self::SYNC_NEED_UPDATE;
        }
        $arrDepartment = explode(',', $dataSubscriber['qy_subscriber_department']);
        if (!in_array($newDepart_id, $arrDepartment)){
            $arrDepartment[] = $newDepart_id;
        }
        $data['qy_subscriber_department'] = implode(',', $arrDepartment);
        $count = $MSubscriber->where($wheres)->save($data);
        if ($count || !$MSubscriber->getError()){
            $res['rcode'] = 1;
            $res['msg'] = 'OK';
        }
        else{
            $res['msg'] = '更新失败';
        }
        return $res;
    }
}

?>